package com.yxw.live_vod_boot.controller;

import com.yxw.live_vod_boot.dto.*;
import com.yxw.live_vod_boot.service.OrderService;
import com.yxw.live_vod_boot.vo.OrderVO;
import com.yxw.live_vod_boot.vo.ShopOrderListVO;
import com.yxw.yxnet_cd_center.common.dto.PageVO;
import com.yxw.yxnet_cd_center.common.helper.ResultHelper;
import com.yxw.yxnet_cd_center.common.utils.BaseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author yxw
 */
@RestController
@Api(value = "订单管理", tags = "订单管理")
@Slf4j
@RequestMapping("/api/order")
public class ShopOrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 查询某一店铺下的订单列表
     */
    @ApiOperation(value = "查询某一店铺下的订单列表")
    @PostMapping(value = "/getOrderListByShopId")
    public BaseResult<PageVO<ShopOrderListVO>> getOrderListByShopId(@RequestBody @Validated ShopOrderListQueryDTO dto) {
        PageVO<ShopOrderListVO> commentList = orderService.getOrderListByShopId(dto);
        return BaseResult.doSuccess(commentList, "查询订单列表成功");
    }

    /**
     * 根据商品id查询订单列表
     */
    @ApiOperation(value = "根据商品id查询订单列表")
    @PostMapping(value = "/get_order_by_product")
    public BaseResult getOrderByProduct(@RequestParam("productId") Long productId) {
        List<OrderVO> voList = orderService.getOrderByProduct(productId);
        return BaseResult.doSuccess(voList,"查询订单列表成功");
    }

    /**
     * 后台获取订单列表
     */
    @ApiOperation(value = "后台获取订单列表")
    @PostMapping(value = "/get_order_list")
    public BaseResult<PageVO<OrderVO>> getOrderList(@RequestBody @Validated OrderQueryDTO queryDTO) {
        PageVO<OrderVO> pageVO = orderService.getOrderList(queryDTO);
        return ResultHelper.fillWithSuccess(pageVO);
    }

    /**
     * 后台获取订单详情
     */
    @ApiOperation(value = "后台获取订单详情")
    @PostMapping(value = "/get_order_by_id")
    public BaseResult getOrderById(@RequestParam("orderId") Long orderId) {
        OrderVO vo = orderService.getOrderById(orderId);
        return BaseResult.doSuccess(vo,"查询订单详情成功");
    }

    /**
     * 获取用户侧订单列表
     */
    @ApiOperation(value = "获取用户侧订单列表")
    @PostMapping(value = "/get_order_list_by_user_id")
    public BaseResult<PageVO<OrderVO>> getOrderListByUserId(@RequestBody @Validated UserOrderQueryDTO queryDTO) {
        PageVO<OrderVO> pageVO = orderService.getOrderListByUserId(queryDTO);
        return ResultHelper.fillWithSuccess(pageVO);
    }

    /**
     * 核销订单券码
     */
    @ApiOperation(value = "核销订单券码")
    @PostMapping(value = "/verifyOrderCouponNum")
    public BaseResult verifyOrderCouponNum(@RequestBody @Validated VerifyOrderCouponNumDTO dto) {
        orderService.verifyOrderCouponNum(dto);
        return BaseResult.doSuccess(null, "核销订单券码成功");
    }

    /**
     * 添加订单
     */
    @ApiOperation(value = "添加订单")
    @PostMapping(value = "/add_order")
    public BaseResult addOrder(@RequestBody @Validated OrderAddDTO addDTO) {
        orderService.addOrder(addDTO);
        return BaseResult.doSuccess(null, "新增订单成功");
    }

    /**
     * 删除订单
     */
    @ApiOperation(value = "删除订单")
    @PostMapping(value = "/delete_order")
    public BaseResult deleteOrder(@RequestParam("orderId") Long orderId) {
        orderService.deleteOrder(orderId);
        return BaseResult.doSuccess(null,"删除成功");
    }

    /**
     * 订单申请退款
     */
    @ApiOperation(value = "订单申请退款")
    @PostMapping(value = "/order_refund")
    public BaseResult orderRefund(@RequestBody @Validated OrderRefundDTO refundDTO) {
        orderService.orderRefund(refundDTO);
        return BaseResult.doSuccess(null,"申请退款成功");
    }

    /**
     * 取消订单
     */
    @ApiOperation(value = "取消订单")
    @PostMapping(value = "/cancel_order")
    public BaseResult cancelOrder(@RequestParam("orderId") Long orderId) {
        orderService.cancelOrder(orderId);
        return BaseResult.doSuccess(null,"取消订单成功");
    }

    /**
     * 订单-使用  支付完成后直接调用
     */
    @ApiOperation(value = "订单-使用")
    @PostMapping(value = "/order_use")
    public BaseResult orderUse(@RequestParam("orderId") Long orderId) {
        orderService.orderUse(orderId);
        return BaseResult.doSuccess(null,"执行成功");
    }

}
